package net.shopxx.action.admin;


import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import net.shopxx.entity.Admin;
import net.shopxx.entity.ProductConsult;
import net.shopxx.service.AdminService;
import net.shopxx.service.ProductConsultService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.util.StringUtil;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

/**
 * 后台Action类 - 商品评论
 */

@ParentPackage("admin")
public class ProductconsultAction extends BaseAdminAction {


	/**
	 * 
	 */
	private static final long serialVersionUID = -3613443621292686669L;
	
	private ProductConsult productConsult;
	private String pid;
	@Resource
	private ProductConsultService productConsultService;

	@Resource
	private AdminService adminService;

	// 列表
	public String list() {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProductConsult.class);
		detachedCriteria.add(Restrictions.eq("product.id", pid));
		pager = productConsultService.findByPager(pager,detachedCriteria);
		return LIST;
	}
	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		productConsult = productConsultService.load(id); 
		return INPUT;
	}
	
	
	
	// 更新
	
	@InputConfig(resultName = "error")
	public String update() throws Exception {
		ProductConsult persistent = productConsultService.load(id); 
		if(!StringUtil.isEmpty(productConsult.getReplycontent())){
			persistent.setReplycontent(productConsult.getReplycontent());
			persistent.setReply(true);
			persistent.setModifyDate(new Date());
			Admin admin = adminService.getLoginAdmin();
			persistent.setAdmin(admin);
		}
		persistent.setStatus(productConsult.getStatus());
		productConsultService.update(persistent);
		flushCache();
		redirectionUrl = "productconsult!list.action?pid="+persistent.getProduct().getId();
		return SUCCESS;
	}
	

	// 删除
	public String delete() throws Exception {
		productConsultService.delete(ids);
		flushCache();
		return ajaxJsonSuccessMessage("删除成功！");
	}
	
	
	public ProductConsult getProductConsult() {
		return productConsult;
	}
	public void setProductConsult(ProductConsult productConsult) {
		this.productConsult = productConsult;
	}
	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}
	

}